package cn.bgs.controller;


import cn.bgs.common.AliOss;
import cn.bgs.common.R;
import cn.bgs.config.DirectConfig;
import cn.bgs.config.DirectlyhConfig;
import cn.bgs.pojo.FeedbackForm;
import cn.bgs.pojo.Sodality;
import cn.bgs.service.SodalityService;
import cn.bgs.util.OrderUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

/**
 * <p>
 * 联谊会表 前端控制器
 * </p>
 *
 * @author Mr.Zhao
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/sodality")
@Slf4j
public class SodalityController {

    @Autowired
    private SodalityService sodalityService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AliOss aliOss;

    @GetMapping("/sodalityinfo")
    @ApiOperation(value = "联谊会信息查询")
    public R sodalityinfo(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String sodTitle){
        QueryWrapper<Sodality> queryWrapper= new QueryWrapper<>();
        queryWrapper.like("sod_title",sodTitle);
        queryWrapper.orderByDesc("create_time").eq("delete_flag",0);
        Page<Sodality> page = sodalityService.page(new Page<>(pageNum, pageSize),queryWrapper);
        return R.ok().data("sodalityinfo" , page);
    }


    @RequestMapping("/addPic")
    @ApiOperation(value = "项目图片")
    public String addPic(MultipartFile file){
        String upload = aliOss.upload(file);
        System.out.println(upload);
        return upload;
    }

    @RequestMapping("/updatedInfo")
    @ApiOperation(value = "联谊会信息审核")
    public R updatedInfo(@RequestBody Sodality sodality){

        try {
            //发送消息到交换机
            //参数： 交换机、路由键、消息体
            rabbitTemplate.convertAndSend(DirectlyhConfig.DIRECT_EXCHANGE_NAME, DirectlyhConfig.DIRECT_ROUTING_KEY_NAME, sodality);
            return R.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error();
        }
    }

    @RequestMapping("/addInfo")
    @ApiOperation(value = "联谊会信息添加")
    public R addInfo(@RequestBody Sodality sodality){
        LocalDateTime localDateTime = LocalDateTime.now();
        sodality.setSodNumber(OrderUtils.getDateTime());
        sodality.setSodTime(localDateTime);
        sodality.setSodState("待审核");
        sodality.setDeleteFlag(0);
        sodality.setCreateTime(localDateTime);
        sodality.setUpdateTime(localDateTime);
        boolean save = sodalityService.save(sodality);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }


    }

    @RequestMapping("/deleteInfo/{id}")
    @ApiOperation(value = "联谊会信息删除")
    public R addInfo(@PathVariable Integer id){

        boolean save = sodalityService.removeById(id);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }


    }


}

